const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
const util = require('../../utils/util.js');

Page({
  data: {
    isloading: true,
    istips:false,
    boxHeght:"",
    bottomLift: 0,
    currdentdetail:'',
    currdentonfo:'',
    page:1,
    pageSize:10,
    isEnd:false,
    contentlistdata:[],
    totalAmount:0,
    totalPreAmount:0,
    price1:0,
    price2:0,
    amount:0
  },
  
  onLoad() {
    let {bottomLift} = app.globalData
    if(bottomLift>=0){
      app.getDeviceSize().then(res => {
        const {bottomLift} = res
        this.setData({
          bottomLift
        })
      })
    }else{
      this.setData({
        bottomLift
      })
    }

  },
  onShow(){
    if (!wx.getStorageSync('logintoken')) {
      app.ToLogin()
    }else{
      this.setData({
        istips:false,
        page:1,
        pageSize:10,
        isEnd:false,
        contentlistdata:[],
      })
      this.orderlist()
    }
    
  },
  totalOrderItemAmount(no){
    Controller.totalOrderItemAmount({orderNumber:no}).then(res=>{
      if(res.data.baseData){
        this.setData({
          totalAmount:res.data.baseData.totalAmount,
          totalPreAmount:res.data.baseData.totalPreAmount,
        })
      }
    })
  },
  orderlist(){
    Controller.orderlist({page:this.data.page,pageSize:this.data.pageSize}).then(res=>{
      if(res.data.baseData && res.data.baseData.list.length>0){
        this.setData({
          contentlistdata:this.data.contentlistdata.concat(res.data.baseData.list),
        })
        var that=this;
        setTimeout(res=>{
          that.getHeght();
        },1000)
        
      }
      if(res.data.baseData.lastPage){
        this.setData({
          isEnd:res.data.baseData.lastPage
        })
      }
      this.setData({
        isloading:false
      })

    })
  },
  onReachBottom(){
    if(this.data.isEnd){
      return
    }else{
      this.setData({
        page:this.data.page+1
      })
      this.orderlist()
    }
    
  },
  tips(e){
    var tips=e.currentTarget.dataset.item;
    var no=e.currentTarget.dataset.ordernumber;
    var item=e.currentTarget.dataset.item;
    this.setData({
      currdentdetail:item
    })
    this.totalOrderItemAmount(no)
    Controller.orderItemList({orderNumber:no}).then(res=>{
      if(res.data.baseData){
        this.setData({
          currdentonfo:res.data.baseData,
        })
        var currdentonfo=this.data.currdentonfo;
        var price1=0,price2=0,amount=0;
        for(var i=0;i<currdentonfo.length;i++){

          if(currdentonfo[i].type==1){
            price1=price1+currdentonfo[i].price;
            console.log(currdentonfo[i].price)
          }
          if(currdentonfo[i].type==2){
            price2=price2+currdentonfo[i].price;
            console.log(currdentonfo[i].price)
          }
          amount=currdentonfo[i].amount
        }

        this.setData({
          price1,
          price2,
          amount
        })
        

        if(this.data.istips){
          this.setData({
            istips:false
          })
          // //设置滚动条位置
          wx.pageScrollTo({
            scrollTop: 0,
            duration: 300
          })
        }else{
          this.setData({
            istips:true
          })

          // //设置滚动条位置
          wx.pageScrollTo({
            scrollTop: e.currentTarget.offsetTop,
            duration: 300
          })

        }
      }
    })
    
  },
  getHeght(){  //获取屏幕高度
    let query = wx.createSelectorQuery();
    var h=wx.getSystemInfoSync().windowHeight

    query.select('.orderitem').boundingClientRect(rect => {
    	  //获取到元素
        let height = rect.height;
        var boxH=h-height

        this.setData({
            boxHeght:boxH
        })
    }).exec();
  },
  close(){
    this.setData({
      istips:false
    })
  },
  golink(e){
    if(e.currentTarget.dataset.type=="swith"){
      wx.switchTab({
        url:e.currentTarget.dataset.url
      });
    }else{
      wx.navigateTo({
        url:e.currentTarget.dataset.url
      });
    }
  },
  refund(e){
    wx.showLoading({
        title: '退款申请中',
      })
    var that=this;
    var no=e.currentTarget.dataset.ordernumber;
    Controller.refundapply({orderNumber:no}).then(res=>{
      that.setData({
        istips:false,
        isloading:true
      })
      
      setTimeout(res=>{
        that.setData({
          istips:false,
          page:1,
          pageSize:10,
          isEnd:false,
          contentlistdata:[],
          isloading:false
        })
        that.orderlist()
        wx.hideLoading()
      },800)
    })
  },
  cancel(e){
    var no=e.currentTarget.dataset.ordernumber;
    var that=this;
    wx.showModal({
      title: '提示',
      content: '是否取消订单',
      success (res) {
        if (res.confirm) {
          Controller.ordercancel({orderNumber:no}).then(res=>{
            wx.showToast({
              title: "取消成功",
              icon: 'none',
              duration: 1000
            });
            that.setData({
              istips:false,
              page:1,
              pageSize:10,
              isEnd:false,
              contentlistdata:[],
            })
            that.orderlist()
          })
        } else if (res.cancel) {
          
        }
      }
    })
  },
  delete(e){
    var no=e.currentTarget.dataset.ordernumber;
    var that=this;
    wx.showModal({
      title: '提示',
      content: '是否删除订单',
      success (res) {
        if (res.confirm) {
          Controller.orderdelete({orderNumber:no}).then(res=>{
            wx.showToast({
              title: "删除成功",
              icon: 'none',
              duration: 1000
            });
            that.setData({
              istips:false,
              page:1,
              pageSize:10,
              isEnd:false,
              contentlistdata:[],
            })
            that.orderlist()
          })
        } else if (res.cancel) {
          
        }
      }
    })
  },
  saveImgBtn(e) {
    var code=e.currentTarget.dataset.code;
    setTimeout(()=>{
      app.saveImg(code)
    },800)
  },
  pay(e){
    var no=e.currentTarget.dataset.ordernumber;
    Controller.pay({ orderNumber: no, payType:1}).then(res => {
      let data=res.data.baseData;
      wx.requestPayment({
        nonceStr: data.nonceStr,
        package: data.package,
        paySign: data.paySign,
        timeStamp: data.timeStamp,
        signType: data.signType,
        success(res){
          wx.showToast({
            title: "支付成功",
            icon: 'none',
            duration: 1000
          });
          this.setData({
            istips:false,
            page:1,
            pageSize:10,
            isEnd:false,
            contentlistdata:[],
          })
          this.orderlist()
        },
        fail(res){
          wx.showToast({
            title: "支付失败",
            icon: 'none',
            duration: 1000
          });
          this.setData({
            istips:false,
            page:1,
            pageSize:10,
            isEnd:false,
            contentlistdata:[],
          })
          this.orderlist()
        }
      })
    }).catch(res=>{
      this.setData({
        istips:false,
        page:1,
        pageSize:10,
        isEnd:false,
        contentlistdata:[],
      })
      this.orderlist()
      wx.showToast({
        title: "支付失败",
        icon: 'none',
        duration: 1000
      });
    })
  },

  onPageScroll: function (e) { // 获取滚动条当前位置
  },
  onShareAppMessage: function (e) {
    
  },
  onShareTimeline: function(res){
    
  },
  onReady(){
    

  }
})